package com.szeastroc.manage.controller.system;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.szeastroc.common.utils.LayerTableDataUtil;
import com.szeastroc.common.utils.Result4Ajax;
import com.szeastroc.core.base.controller.BaseController;
import com.szeastroc.core.bean.system.SystemRole;
import com.szeastroc.core.bean.system.SystemUser;
import com.szeastroc.core.query.system.SystemUserQuery;
import com.szeastroc.core.service.system.SystemRoleService;
import com.szeastroc.core.service.system.SystemUserService;

@Controller
@RequestMapping("/systemUser")
public class SystemUserController extends BaseController {

    @Autowired
    private SystemUserService userService;

    @Autowired
    private SystemRoleService systemRoleService;


    @GetMapping("/index")
    public String userIndex(Model model) {
        List<SystemRole> roles = systemRoleService.selectAll();
        model.addAttribute("roles", roles);
        return "system/system_user/system_user.html";
    }

    @GetMapping("/toPage/system_user_add")
    public String toPage(Model model) {
        List<SystemRole> roleList = systemRoleService.selectAll();
        model.addAttribute("roles", roleList);
        return "system/system_user/system_user_add.html";
    }

    @PostMapping("/getUserList")
    @ResponseBody
    public LayerTableDataUtil<SystemUser> getUserList(Model model, SystemUserQuery query) {
        PageInfo<SystemUser> pageinfo = userService.selectPage(query);
        LayerTableDataUtil<SystemUser> data = new LayerTableDataUtil<SystemUser>(
                pageinfo.getTotal(), pageinfo.getList());
        return data;
    }

    @PostMapping("/setUserRole")
    @ResponseBody
    public Result4Ajax setUserRole(Integer userId, Integer roleId,
                                   Integer oldRoleId) {
        try {
            userService.setUserRole(userId, roleId, oldRoleId);
            return super.success();
        } catch (Exception e) {
            logger.error("修改用户角色错误!", e);
            return super.error();
        }
    }

    @PostMapping("/deleteUserRole")
    @ResponseBody
    public Result4Ajax deleteUserRole(Integer userId, Integer roleId) {
        try {
            userService.deleteUser(userId, roleId);
            return super.success();
        } catch (Exception e) {
            logger.error("修改用户角色错误!", e);
            return super.error();
        }
    }

    @PostMapping("addUser")
    @ResponseBody
    public Result4Ajax addUser(SystemUser user) {
        System.out.println("调用接口");
        try {
            if (user == null) {
                return super.error("用户不能为空");
            }

            if (StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getIdCard())
                    || StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(user.getMobile()) || user.getRoleId() == null) {
                return super.error("缺少参数");
            }

            SystemUserQuery query = new SystemUserQuery();
            query.setMobile(user.getMobile());
            PageInfo<SystemUser> pageinfo = userService.selectPage(query);
            if(pageinfo != null && pageinfo.getTotal() > 0){
                return super.error("电话号码重复");
            }


            query.setMobile(null);
            query.setUsername(user.getUsername());
            PageInfo<SystemUser> pageinfo2 = userService.selectPage(query);
            if(pageinfo2 != null && pageinfo2.getTotal() > 0){
                return super.error("用户名已经存在");
            }


            query.setMobile(null);
            query.setUsername(null);
            query.setIdCard(user.getIdCard());
            PageInfo<SystemUser> pageinfo3 = userService.selectPage(query);
            if(pageinfo3 != null && pageinfo3.getTotal() > 0){
                return super.error("用户名已经存在");
            }

            userService.saveUser(user);

            return super.success();
        } catch (Exception e) {
            logger.error("修改用户角色错误!", e);
            return super.error();
        }
    }
}
